import java.util.Iterator;

import edu.stanford.nlp.ling.Sentence;
import edu.stanford.nlp.ling.TaggedWord;
import edu.stanford.nlp.tagger.maxent.MaxentTagger;

public class POSTagger {
	private static String modelFileName = "data/POSTaggerModels/left3words-wsj-0-18.tagger";
	private static POSTagger instance;
	
	public static synchronized POSTagger getInstance() {
		if (instance == null){
			instance = (POSTagger) Util.loadObject("POSTagger");
			
			if(instance == null)			
				instance = new POSTagger();
		}
		
		return instance;
	}
	
	private POSTagger(){
		try {
			MaxentTagger.init(modelFileName);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public TaggedWordExt POSTagWord(String word){
		Sentence<TaggedWord> tSentence;
		try {
			tSentence = MaxentTagger.tagStringTokenized(word);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		Iterator<TaggedWord> it = tSentence.iterator();
		if(it.hasNext()){
			TaggedWord tWord = it.next();
			TaggedWordExt tWordExt = new TaggedWordExt(tWord);
			return tWordExt;
		}
		return null;
	}
	
}
